package biz.obake.team.android;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class c {
    public static void a(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException unused) {
            }
        }
    }

    public static byte[] b(InputStream inputStream, int i) {
        byte[] bArr = new byte[i];
        int i2 = 0;
        while (i2 < i) {
            int read = inputStream.read(bArr, i2, i - i2);
            if (read == -1) {
                break;
            }
            i2 += read;
        }
        if (i2 >= i) {
            return bArr;
        }
        byte[] bArr2 = new byte[i2];
        System.arraycopy(bArr, 0, bArr2, 0, i2);
        return bArr2;
    }

    /* JADX INFO: finally extract failed */
    public static String c(String str) {
        StringBuffer stringBuffer = new StringBuffer();
        try {
            FileInputStream openFileInput = a.a().openFileInput(str);
            char[] cArr = new char[1024];
            InputStreamReader inputStreamReader = new InputStreamReader(openFileInput, "utf8");
            while (true) {
                int read = inputStreamReader.read(cArr);
                if (-1 != read) {
                    stringBuffer.append(cArr, 0, read);
                } else {
                    a(openFileInput);
                    return stringBuffer.toString();
                }
            }
        } catch (Throwable th) {
            a((Closeable) null);
            throw th;
        }
    }

    public static void d(InputStream inputStream, String str) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = a.a().openFileOutput(str, 0);
            byte[] bArr = new byte[1024];
            while (true) {
                int read = inputStream.read(bArr);
                if (-1 != read) {
                    fileOutputStream.write(bArr, 0, read);
                } else {
                    return;
                }
            }
        } finally {
            a(fileOutputStream);
        }
    }

    public static void e(String str, String str2) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = a.a().openFileOutput(str2, 0);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf8");
            outputStreamWriter.write(str);
            outputStreamWriter.close();
            a(fileOutputStream);
        } catch (FileNotFoundException e) {
            throw e;
        } catch (UnsupportedEncodingException e2) {
            throw e2;
        } catch (Throwable th) {
            a(fileOutputStream);
            throw th;
        }
    }
}
